layui.use(['table', 'jquery', 'layer', 'upload'], function () {
    var table = layui.table,
        layer = layui.layer,
        upload = layui.upload,
        $ = layui.jquery;
    var username = $('input[name="userName"]').val();
    var isLeader = $('input[name="leader"]').val();

    table.render({
        elem: "#productionTask",
        height: 'full-125',
        url: ctx + '/design/selectByParams',
        page: true,
        cellMinWidth: 80, // 单元格最小宽度
   //     toolbar: '#toolbarDemo',
        cols: [[ // 表头
            {type: 'checkbox'},
            {type: "numbers"},
            {
                title: "项目编号", templet: function (data) {
                    return data.projectBean.projectNo;
                }
            },
            {
                title: "产品", templet: function (data) {
                    return data.projectBean.basicQuotationBean.productName;
                }
            },
            {
                title: "数量", width: 100, templet: function (data) {
                    return data.projectBean.basicQuotationBean.productCount;
                }
            }, {
                title: "计划交付日期", width: 150, templet: function (data) {
                    return data.projectBean.basicQuotationBean.deliveryDate;
                }
            }, {
                title: "规格", templet: function (data) {
                    return data.projectBean.basicQuotationBean.description;
                }
            }, {
                title: "研发人员", width: 120, templet: function (data) {
                    if (data.technologyEmp == null) {
                        return "暂无"
                    } else {
                        return data.technologyEmp.empName;
                    }
                }
            },
            {
                title: "状态", templet: function (data) {
                    if (data.status == 0) {
                        return "未指派"
                    } else if (data.status == 1) {
                        return "设计中"
                    } else if (data.status == 2) {
                        return "已完成"
                    }

                }
            }
            ,
            {
                title: "操作", templet: function (data) {
                    if(data.status == 0 && isLeader ==1){
                        return '<a class="layui-btn  layui-btn-warm   layui-btn-xs " style="margin-left: 2px" lay-event="downloadFile"' +
                            '  value=" ' + data.id + '  "  >下载</a> <a class="layui-btn layui-btn-xs " style="margin-left: 2px" lay-event="assign"' +
                            '  value=" ' + data.id + '  "  >指派</a>';
                    }
                    if (data.technologyEmp == null || data.technologyEmp == "" && data.status==0) {
                        return '<a class="layui-btn  layui-btn-warm   layui-btn-xs " style="margin-left: 2px" lay-event="downloadFile"' +
                            '  value=" ' + data.id + '  "  >下载</a> +      '
                    } else {
                        var empName = new String(data.technologyEmp.empName);

                        if (empName != null && empName != '') {
                            if (empName.localeCompare(username) == 0) {
                                return '<a class="layui-btn  layui-btn-warm   layui-btn-xs " style="margin-left: 2px" lay-event="downloadFile"' +
                                    '  value=" ' + data.id + '  "  >下载</a>'
                                    + '<a  class="layui-btn layui-btn-primary  layui-btn-xs" lay-event="addBom"  id="uploadExcel">导入</a>'
                            } else {
                                return '<a class="layui-btn  layui-btn-warm   layui-btn-xs " style="margin-left: 2px" lay-event="downloadFile"' +
                                    '  value=" ' + data.id + '  "  >下载</a>'
                            }

                        } else {
                            return '<a class="layui-btn  layui-btn-warm   layui-btn-xs " style="margin-left: 2px" lay-event="downloadFile"' +
                                '  value=" ' + data.id + '  "  >下载</a>'
                        }
                    }

                }
            }
        ]] ,


    })

    //监听头部
    // table.on('toolbar(productionTaskFilter)', function (data) {
    //     if (data.event == "assign") {
    //         var checkStatus = table.checkStatus(data.config.id);
    //         openAssignDialog(checkStatus.data);
    //     }
    // });
    // 监听行
    table.on('tool(productionTaskFilter)', function (data) {
          if (data.event =="assign"){
              openAssignDialog(data.data);
          }
        if (data.event == "downloadFile") {

            window.location.href = ctx + "/file/downloadFile?id=" + data.data.projectBean.basicQuotationBean.id
        }
        if (data.event == "addBom") {
            console.log(data.data)
            layer.open({
                type: 2,
                content: ctx + "/design/toAddExcel?id=" + data.data.id + "&&projectId=" + data.data.projectId,
                area: ['400px', '400px'],
                shadeClose: false,
                title: "导入物料单"
            })
        }

    })


    function openAssignDialog(data) {
        console.log(data)
        if (data.status != 0){
            layer.msg("已经指派过人员了" ,{icon:6})
            return;
        }
        layer.open({
            type: 2,
            area: ['600px', '500px'],
            shadeClose: true,
            maxmin: true,
            title: "<h3>研发订单指派</h3>",
           content: ctx + "/design/assign?projectId=" + data.projectId
        })
    }


});